//
//  YMCommentViewController.m
//  YMCommentDemo
//
//  Created by lantaiyuan on 2017/2/24.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "YMCommentViewController.h"
#import "YMCommentView.h"
#import "YMCommentContentView.h"
#import "YMCommentDriverView.h"
#import "YMCommentStationFacilityView.h"
#import "YMCommentEmergencyView.h"
#import "YMCommentStationActivityView.h"
#import "YMCommentWayStationView.h"

#define SYS_SIZE [UIScreen mainScreen].bounds.size

@interface YMCommentViewController ()<YMCommentContentViewDelegate>
@property (nonatomic, strong) YMCommentView * commentView;
@property (nonatomic, strong) YMCommentContentView * commentContenView;
@property (nonatomic, strong) YMCommentDriverView * driverView;
@property (nonatomic, strong) YMCommentStationFacilityView * stationFacilityView;
@property (nonatomic, strong) YMCommentEmergencyView * emergencyView;
@property (nonatomic, strong) YMCommentStationActivityView * stationActivityView;
@property (nonatomic, strong) YMCommentWayStationView * wayStaionView;


@property (nonatomic, strong) UIView * maskView;
@property (nonatomic, strong) UIView * feedbackAlerView;
@property (nonatomic, strong) UIView * keyboardToolView;
@end

@implementation YMCommentViewController

#pragma mark - Lazy

- (UIView *)commentView{
    if (!_commentView) {
        _commentView = [[YMCommentView alloc] initWithFrame:CGRectMake(0, SYS_SIZE.height - 350, SYS_SIZE.width, 350)];
    }
    return _commentView;
}

- (UIView *)commentContenView{
    if (!_commentContenView) {
        _commentContenView = [[YMCommentContentView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width, 350)];
        _commentContenView.delegate = self;
    }
    return _commentContenView;
}

- (YMCommentDriverView *)driverView{
    if (!_driverView) {
        _driverView = [[YMCommentDriverView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width, 350)];
    }
    return _driverView;
}

- (YMCommentStationFacilityView *)stationFacilityView{
    if (!_stationFacilityView) {
        _stationFacilityView = [[YMCommentStationFacilityView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width, 350)];
    }
    return _stationFacilityView;
}

- (YMCommentEmergencyView *)emergencyView{
    if (!_emergencyView) {
        _emergencyView = [[YMCommentEmergencyView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width, 350)];
    }
    return _emergencyView;
}

- (YMCommentStationActivityView *)stationActivityView{
    if (!_stationActivityView) {
        _stationActivityView = [[YMCommentStationActivityView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width, 350)];
    }
    return _stationActivityView;
}

- (YMCommentWayStationView *)wayStaionView{
    if (!_wayStaionView) {
        _wayStaionView = [[YMCommentWayStationView alloc] initWithFrame:CGRectMake(0, 00, SYS_SIZE.width, 350)];
    }
    return _wayStaionView;
}

- (UIView *)keyboardToolView{
    if (!_keyboardToolView) {
        _keyboardToolView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height, self.view.width, 44)];
        _keyboardToolView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        UIButton * doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width - 44, 0, 44, 44)];
        [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
        [doneBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        doneBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [doneBtn addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchDown];
        [_keyboardToolView addSubview:doneBtn];
        
        [_keyboardToolView addTopLineWithColor:HEX(@"#dddddd")];
    }
    return _keyboardToolView;
}

- (UIView *)maskView{
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:self.view.bounds];
        _maskView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.5];
        
    }
    return _maskView;
}

- (void)alertViewWithImage:(NSString *)imageStr content:(NSString *)content{
    UIView * alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SYS_SIZE.width - 90, 300)];
    alertView.backgroundColor = [UIColor whiteColor];
    
    NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithString:content];
    text.font = [UIFont systemFontOfSize:12];
    text.lineSpacing = 4;
    text.firstLineHeadIndent = 25;
    
    YYTextView * textView = [[YYTextView alloc] init];
    textView.editable = NO;
    textView.textColor = HEX(@"#333333");
    textView.font = [UIFont systemFontOfSize:12];
    textView.attributedText = text;
    textView.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    textView.size = [self sizeWithContent:content font:textView.font width:alertView.width];
    textView.width = alertView.width;
    [alertView addSubview:textView];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(textView.frame), alertView.width, 180)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:imageStr];
    [alertView addSubview:imageView];
    
    alertView.height = CGRectGetMaxY(imageView.frame);
    alertView.center = self.view.center;
    [self.view addSubview:alertView];
    
    alertView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    alertView.layer.shadowOffset = CGSizeMake(0, 0);
    alertView.layer.shadowOpacity = 0.3;
    alertView.layer.shadowRadius = 0.7;
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(alertView.frame) + 15, 22, 22)];
    closeBtn.centerX = alertView.centerX;
    [closeBtn setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnAction:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:closeBtn];
}

- (CGSize)sizeWithContent:(NSString *)content font:(UIFont *)font width:(CGFloat)width
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    paragraphStyle.lineSpacing = 10;  // 段落高度
    
    //    NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle};

    NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:content];
    
    [attributes addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, content.length)];

    [attributes addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];

    CGSize attSize = [attributes boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    return attSize;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        
    [self.view addSubview:self.maskView];
    [self.view addSubview:self.commentView];
    
    [self keyboard];
    
    self.commentView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    self.commentView.layer.shadowOffset = CGSizeMake(0, 0);
    self.commentView.layer.shadowOpacity = 0.3;
    self.commentView.layer.shadowRadius = 0.7;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSendNotification:) name:Comment_send_action object:nil];
    
    
    __weak typeof(self) weakSelf = self;
    self.commentView.cancelBtnActionBlock = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cancel)]) {
            [weakSelf.delegate cancel];
        }
    };
    
    self.driverView.backBlock = ^{
        [weakSelf.driverView removeFromSuperview];
    };
    
    self.stationFacilityView.backBlock = ^{
        [weakSelf.stationFacilityView removeFromSuperview];
    };
    
    self.emergencyView.backBlock = ^{
        [weakSelf.emergencyView removeFromSuperview];
    };
    
    self.stationActivityView.backBlock = ^{
        [weakSelf.stationActivityView removeFromSuperview];
    };
    
    self.wayStaionView.backBlock = ^{
        [weakSelf.wayStaionView removeFromSuperview];
    };
    
    // 站台
    self.commentView.selectStationCommentItemBlock = ^(StationCommentType type){
        switch (type) {
            case StationCommentTypeCrowd:
            {
                NSLog(@"StationCommentTypeCrowd");
                weakSelf.commentContenView.commentType = YMCommentContentViewTypeCrown;
                [weakSelf.commentView addSubview:weakSelf.commentContenView];
            }
                break;
            case StationCommentTypeFacility:
            {
                NSLog(@"StationCommentTypeFacility");
                [weakSelf.commentView addSubview:weakSelf.stationFacilityView];
            }
                break;
            case StationCommentTypeEmergency:
            {
                NSLog(@"StationCommentTypeEmergency");
                [weakSelf.commentView addSubview:weakSelf.emergencyView];
            }
                break;
            case StationCommentTypeClean:
            {
                NSLog(@"StationCommentTypeClean");
                weakSelf.commentContenView.commentType = YMCommentContentViewTypeClean;
                [weakSelf.commentView addSubview:weakSelf.commentContenView];
            }
                break;
            case StationCommentTypeActivity:
            {
                NSLog(@"StationCommentTypeActivity");
                [weakSelf.commentView addSubview:weakSelf.stationActivityView];
            }
                break;
                
            default:
                break;
        }
    };
    
    // 车辆
    self.commentView.selectVehicleCommentItemBlock = ^(VehicleCommentType type){
        switch (type) {
            case VehicleCommentTypeOverMiddleStation:
            {
                NSLog(@"VehicleCommentTypeOverMiddleStation");
                [weakSelf.commentView addSubview:weakSelf.wayStaionView];
            }
                break;
            case VehicleCommentTypeCrowd:
            {
                NSLog(@"VehicleCommentTypeCrowd");
                weakSelf.commentContenView.commentType = YMCommentContentViewTypeCrown;
                [weakSelf.commentView addSubview:weakSelf.commentContenView];
            }
                break;
            case VehicleCommentTypeEmergency:
            {
                NSLog(@"VehicleCommentTypeEmergency");
                [weakSelf.commentView addSubview:weakSelf.emergencyView];
            }
                break;
            case VehicleCommentTypeDriver:
            {
                NSLog(@"VehicleCommentTypeDriver");
                [weakSelf.commentView addSubview:weakSelf.driverView];
            }
                break;
            case VehicleCommentTypeClean:
            {
                NSLog(@"VehicleCommentTypeClean");
                weakSelf.commentContenView.commentType = YMCommentContentViewTypeClean;
                [weakSelf.commentView addSubview:weakSelf.commentContenView];
            }
                break;
            case VehicleCommentTypeTemperature:
            {
                NSLog(@"VehicleCommentTypeTemperature");
                
                weakSelf.commentContenView.commentType = YMCommentContentViewTypeTempreture;
                [weakSelf.commentView addSubview:weakSelf.commentContenView];
            }
                break;
                
            default:
                break;
        }
    };
}


- (void)receiveSendNotification:(NSNotification *)notification{
    
    YMCommentSendType sendType = [notification.userInfo[@"sendtype"] integerValue];
    
    switch (sendType) {
        case YMCommentSendTypeWayStation:
        {
            NSLog(@"未停靠途经站点");
        }
            break;
        case YMCommentSendTypeCrown:
        {
            NSLog(@"拥挤情况");
        }
            break;
        case YMCommentSendTypeEmergency:
        {
            NSLog(@"突发事件");
        }
            break;
        case YMCommentSendTypeDriverServer:
        {
            NSLog(@"司机服务");
        }
            break;
        case YMCommentSendTypeClean:
        {
            NSLog(@"整洁情况");
        }
            break;
        case YMCommentSendTypeTempreture:
        {
            NSLog(@"车内温度");
        }
            break;
        case YMCommentSendTypeFacility:
        {
            NSLog(@"车站设施");
        }
            break;
        case YMCommentSendTypeActivity:
        {
            NSLog(@"大型活动");
        }
            break;
            
        default:
            break;
    }
    
    [self.commentView removeFromSuperview];
    [self alertViewWithImage:@"01" content:@"感谢您的热心反馈，请尽快告知司机，我们会马上跟进处理！同时请您照顾好自己，如有能力，也请给需要帮助的群体给予力所能及的帮助，安全公交，感恩有您！"];
}


#pragma mark - 键盘处理
- (void)keyboard{
    
    //注册键盘出现的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    //注册键盘消失的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    
    [self.view insertSubview:self.keyboardToolView aboveSubview:self.commentView];
    
    CGRect keyBoardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.commentView.y = self.view.height - 350 - 184;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.keyboardToolView.y = self.view.height - 44 - keyBoardFrame.size.height;
    }];
}

-(void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    self.commentView.y = self.view.height - 350;
    [UIView animateWithDuration:0.25 animations:^{
        self.keyboardToolView.y = self.view.height;
        [self.keyboardToolView removeFromSuperview];
    }];
}

#pragma mark - Private

- (void)closeBtnAction:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancel)]) {
        [self.delegate cancel];
    }
}

- (void)endEdit{
    [self.view endEditing:YES];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - YMCommentContentViewDelegate
- (void)commentContentView:(YMCommentContentView *)commentContentView didClickBackBtn:(UIButton *)btn{
    NSLog(@"___________Delegate______");
    [self.commentContenView removeFromSuperview];
    self.commentContenView = nil;
}
@end
